package com.zzxx.mapper;

import com.zzxx.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RoleMapper {
    @Select("select * from role")
    List<Role> findAll();

    @Select("select * from role where id in(select roleId from users_role where userId = #{uid})")
    @Results(id = "roleMap", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "roleName", property = "roleName"),
            @Result(column = "roleDesc", property = "roleDesc"),
            @Result(column = "id", property = "permissions",
                    many = @Many(select = "com.zzxx.mapper.PermissionMapper.findByRId")),
    })
    List<Role> findByUid(String uid);

    @Select("select * from role where id not in(" +
            "select roleId from users_role where userId = #{uid})")
    List<Role> findOtherRolesById(String uid);

    @Insert("insert into users_role(roleId,userId) values(#{roleId}, #{userId})")
    void addRole(@Param("roleId") String roleId, @Param("userId") String userId);

    @Insert("insert into role(id,roleName,roleDesc) values(#{id}, #{roleName}, #{roleDesc})")
    void add(Role role);

}
